<template>
  <template v-if="user">
      <van-cell title="昵称" :value="user.userName" @click="toEdit('userName' ,'昵称', user.userName ? user.userName : '')"/>
      <van-cell title="账号" :value="user.userAccount" />
    <van-cell title="头像" >
      <img style="height: 48px" :src="user.userAvatar"/>
    </van-cell>
      <van-cell title="性别" is-link :value="user.sex" @click="toEdit('sex' ,'性别', user.sex ? user.sex : '')"/>
      <van-cell title="电话" is-link :value="user.phone" @click="toEdit('phone' ,'电话',user.phone ? user.phone : '')"/>
      <van-cell title="邮箱" is-link :value="user.email" @click="toEdit('email' ,'邮箱',user.email ? user.email : '')"/>
    <van-cell title="创建时间"  :value="formattedTime"/>
    <van-cell v-if="user.userRole === 'admin' || user.userRole === 'superAdmin'" title="进入后台管理界面"  is-link to="admin/dashboard" />
  </template>
</template>

<script setup>
import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import myAxios from "../../plugins/myAxios";
import {showFailToast, showSuccessToast} from "vant";
import {getCurrentUser} from "../services/user";

// const user = {
//   id: 1,
//   username: '蓝莓',
//   userAccount: 'lanMei',
//   avatarUrl: 'src/img/yln.jpg',
//   gender: '男',
//   phone: '123456',
//   email: '123456@qq.com',
//   planetCode: '1',
//   createTime: new Date(),
// }
const user = ref();
const formattedTime = ref('');
onMounted(async () => {
  const res= await getCurrentUser();
  user.value = res.data;
  const originalDate = new Date(user.value.createTime);
  const year = originalDate.getFullYear();
  const month = originalDate.getMonth() + 1; // 月份是从 0 开始的，所以要加1
  const day = originalDate.getDate();
  const hours = originalDate.getHours();
  const minutes = originalDate.getMinutes();
  const seconds = originalDate.getSeconds();
  formattedTime.value = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${hours < 10 ? '0' + hours : hours}:${minutes < 10 ? '0' + minutes : minutes}:${seconds < 10 ? '0' + seconds : seconds}`;
})


const router = useRouter();

const toEdit = (editKey,editName,currentValue) =>{
  router.push({
    path:'/user/edit',
    query:{
      editKey,
      editName,
      currentValue,
    }
  })
}
</script>

<style scoped>

</style>